﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
namespace Persistence
{
    public abstract class BaseMap
    {
        private static ISqlMapper mapper = null;
        private static object locker = new object();
        private static IBatisNet.Common.Utilities.ConfigureHandler handler ;//= rebuildMap;
        private static void rebuildMap(object a)
        {
            DomSqlMapBuilder builder = new DomSqlMapBuilder();
            string fileName = System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["ConfigPath"]);
            mapper = builder.ConfigureAndWatch(fileName + "sqlmap_" + ConfigurationManager.AppSettings["sqltype"] + ".config", handler);
        }
        private static void InitMap(object a)
        {
            mapper = null;
        }

        public BaseMap()
        {
            handler = new IBatisNet.Common.Utilities.ConfigureHandler(InitMap);
        }
        public static ISqlMapper Mapper
        {
            get
            {
                if (mapper == null)
                {
                    lock (locker)
                    {
                        if (mapper == null)
                        {
                            rebuildMap(null);
                        }
                    }
                }
                return mapper;
            }
        }
    }
}
